<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../demo/demo.css">
    <script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript" src="../jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../jquery.serialize-object.js"></script>
    <script type="text/javascript" src="../xheditor/xheditor-1.2.2.min.js"></script>
    <script type="text/javascript" src="../xheditor/xheditor_lang/zh-cn.js"></script>
    <script type="text/javascript" src="../jquery.parseurl.min.js"></script>
</head>
<body>
<table id="dg_comment">

</table>

<div id="add_comment" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save', closed: true" style="width:100%;height:280px;padding:10px">
        <form id="comment_ff" class="easyui-form" data-options="novalidate:true">
            <div style="margin-bottom:10px">
                <input class="easyui-textbox" name="content" style="width:80%" data-options="label:'内容:',required:true">
            </div>
            <div style="margin-bottom:10px">
                <input class="easyui-textbox" name="appraiser" style="width:80%" data-options="label:'评价人:',required:true">
            </div>
        </form>
        <div style="text-align:center;padding:5px 0">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="comment_submitForm()" style="width:80px">Submit</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="comment_clearForm()" style="width:80px">Clear</a>
        </div>
    </div>
</body>
</html>
<script>
    var articleId=$.parseUrl(window.location.href).query.newsid;
    function del_comment(ee) {
        $.messager.confirm('提示', '你确定要删除吗?', function(b){
            if (b) {
                var id=$('#dg_comment').datagrid('getSelections');
                console.log(id[0]._id);
                $.ajax({
                    url: 'http://localhost:3000/comment/data/' + id[0]._id,
                    method: 'delete'
                }).done(function (res) {
                    $('#dg_comment').datagrid('reload');
                })
            }else {
                $('#dg_comment').datagrid('clearChecked');
            }
        });
    }
    function comment_submitForm(){
        $('#comment_ff').form('submit',{
            onSubmit:function(){
                if($(this).form('enableValidation').form('validate')){
                    var data=$('#comment_ff').serializeObject();
                    data.articleId=articleId;
                    console.log(data);
                    $.ajax({
                        method:"post",
                        url:"http://localhost:3000/comment/data",
                        data:data,
                    }).done(function (res) {
                        $('#add_comment').dialog('close');
                        $('#dg_comment').datagrid('reload');
                    });
                }else{
                    $.messager.alert('提示','请重新输入用户信息!');
                }
            }
        });
    }
    function initList(articleId){
//        var postUrl;
        if(articleId!=undefined&&articleId.trim().length>0){
            queryParams={articleId:articleId}
        }else{
            queryParams={}
        }

        $('#dg_comment').datagrid({
            url:'http://localhost:3000/comment/list',
            //data:{newsId:newsId},
            method:'post',
            queryParams:queryParams,
            pagination:true,
            toolbar:toolbar,
            fit:true,
            columns:[[
                {field:'ck',checkbox:true},
                {field:'articleId',title:'ID',width:150,align:'center'},
                {field:'content',title:'内容',width:150,align:'center'},
                {field:'appraiser',title:'评价人',width:100,align:'center'},
                {field:'opt',title:'操作',width:100,align:'center',
                    formatter: function(value,row,index){
                        return " <a href='javascript:void(0)' name='del' class='easyui-linkbutton' onclick='del_comment(this)'>删除</a>";
                    }
                },

            ]],
            onLoadSuccess:function(data){
                $("a[name='del']").linkbutton({text:'删除',plain:true,iconCls:'icon-cut'});
            },
        });

    }
//    $(function () {
        var toolbar=[
            {
                text:'Add',
                iconCls:'icon-add',
                handler:function(){
                    $('#add_comment').dialog('open');
                    $('#comment_ff input').val("");

//                    var s=$('#dg_comment').datagrid('getRows');
//                    console.log(s[0]);
                }
            },{
                text:'Cut',
                iconCls:'icon-cut',
                handler:function(){
                    var s=$('#dg_comment').datagrid('getSelections');
                    var arr=[];
                    var len=s.length;
                    for(var i=0;i<len;i++){
                        arr.push(s[i]._id);
                    }
                    if(arr.length>0){
                        var data=arr.toString();
                        $.messager.confirm('提示', '你确定要删除吗?', function(b){
                            if (b){
                                $.ajax({
                                    url:"http://localhost:3000/comment/deletes",
                                    method:"post",
                                    data:{idarr:data}
                                }).done(function (res) {
                                    console.log(res.status);
                                    if(res.status==200){
                                        $.messager.alert('提示','删除成功!');
                                    }else {
                                        $.messager.alert('提示','请选择要删除的数据！','warning');
                                    }
                                    $('#dg_comment').datagrid('reload');
                                })
                            }
                        });

                    }else {
                        $.messager.alert('提示','请选择要删除的数据！','warning');
                    }
                }
            },
            {
                text:'Search',
                iconCls:'icon-search',
                handler:function(){
                    $.messager.prompt('提示', '请输入您要搜索的内容：', function(r){
                        if (r){
                            $('#dg_comment').datagrid('load', {
                                content: r,
                                address: 'ho',
                            });
                            $('#dg_comment').datagrid('reload');

                        }
                    });
                }
            },
            {
                text:'刷新',
                iconCls:'icon-reload',
                handler:function(){
                    $('#dg_comment').datagrid('load', {
                        content: '',
                        address: 'ho',
                    });
                    $('#dg_comment').datagrid('reload');
                }
            }
        ];

//    })
    initList(articleId)
</script>